package com.doform.android.xianggucheng.activity;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.bean.GuidBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddMeasureUnitBean;
import com.doform.android.xianggucheng.retrofit.ApiSpecTransformer.DialogTransformer;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.utils.TsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-08-01.
 * GitHub：https://github.com/cxydxpx
 */

public class NewMeasureUnitActivity extends BaseTitleActivity implements TextView.OnEditorActionListener {

    @BindView(R.id.et_name)
    EditText et_name;

    @BindView(R.id.et_sort)
    EditText et_sort;

    @BindView(R.id.tv_name)
    TextView tv_name;

    @BindView(R.id.tv_sort)
    TextView tv_sort;

    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;

    @Override
    protected int layoutResId() {
        return R.layout.activity_common_sort;
    }

    @Override
    protected void initListener() {
        super.initListener();
        et_name.setOnEditorActionListener(this);
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle(getResources().getString(R.string.unit_add_title));

        tv_name.setText(getResources().getString(R.string.unit_tv_name));
        tv_sort.setText(getResources().getString(R.string.default_tv_sort));

        et_name.setHint(getResources().getString(R.string.unit_name_hint));
        et_sort.setText(getResources().getString(R.string.default_et_sort));
    }

    private void beforeSubmit() {

        String mName = et_name.getText().toString().trim();
        String mSort = et_sort.getText().toString().trim();

        if (TextUtils.isEmpty(mName)) {
            TsUtils.showToast("请输入计量单位");
            return;
        }
        postDataForService(mName, mSort);
    }

    private void postDataForService(String mName, String mSort) {
        HttpManager
                .instance()
                .addMeasureUnit(new ReqAddMeasureUnitBean(mName, mSort))
                .compose(new DialogTransformer<GuidBean>(mProgressBar))
                .subscribe(new ErrorHandleSubscriber<GuidBean>() {
                               @Override
                               public void onNext(@NonNull GuidBean str) {
                                   finish();
                               }
                           }
                );
    }

    @OnClick(R.id.btn_submit)
    public void submit() {
        beforeSubmit();
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        switch (v.getId()) {
            case R.id.et_name:
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    beforeSubmit();
                }
                break;
        }


        return false;
    }
}
